import 'package:dio/dio.dart';
import 'package:flutter/widgets.dart';

/*
 * 创建人： zhaollong
 * 创建时间：2019-09-02
 * 页面说明：dio 网络请求拦截日志
 * 功能性修改记录：
 */
class LogsInterceptors extends InterceptorsWrapper {
  @override
  void onRequest(
    RequestOptions options,
    RequestInterceptorHandler handler,
  ) {
    super.onRequest(options, handler);
    debugPrint("================== 请求数据 ==========================");
    debugPrint("|请求url：${options.path}");
    debugPrint('|请求头: ' + options.headers.toString());
    debugPrint('|请求参数: ' + options.queryParameters.toString());
    debugPrint('|请求方法: ' + options.method);
    debugPrint("|contentType = ${options.contentType}");
    debugPrint('|请求时间: ' + DateTime.now().toString());
    if (options.data != null) {
      debugPrint('|请求数据体: ' + options.data.toString());
    } else {
      debugPrint("|请求数据体为 null");
    }
  }

  @override
  void onResponse(
    Response response,
    ResponseInterceptorHandler handler,
  ) {
    super.onResponse(response, handler);
    debugPrint("\n|================== 响应数据 ==========================");
    if (response != null) {
      debugPrint("|url = ${response.realUri.path}");
      debugPrint("|code = ${response.statusCode}");
      debugPrint("|data = ${response.data}");
      debugPrint('|返回时间: ' + DateTime.now().toString());
      debugPrint("\n");
    } else {
      debugPrint("|data = 请求错误 E409");
      debugPrint('|返回时间: ' + DateTime.now().toString());
      debugPrint("\n");
    }
  }

  @override
  void onError(
    DioError e,
    ErrorInterceptorHandler handler,
  ) {
    super.onError(e, handler);
    debugPrint("\n================== 错误响应数据 ======================");
    debugPrint("|type = ${e.type}");
    debugPrint("|message = ${e.message}");

    debugPrint('|response = ${e.response}');
    debugPrint("\n");
  }
}
